package com.adingxiong.cft.controller;


import com.adingxiong.cft.entity.Role;
import com.adingxiong.cft.errors.Assert;
import com.adingxiong.cft.result.Result;
import com.adingxiong.cft.service.IRoleService;
import com.adingxiong.cft.service.IUserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author xiongc
 * @since 2020-09-25
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色管理")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @PostMapping("/role")
    @ApiOperation(value = "创建角色")
    public Result add (@RequestBody Role role){
        boolean insert = roleService.add(role);
        return insert ? Result.success(insert) :Result.failed();
    }

    @GetMapping("/roles/{id}")
    @ApiOperation(value = "查看角色信息")
    public Result<Role> getOne(@PathVariable Integer id){
        return Result.success(roleService.getById(id));
    }
    @DeleteMapping("/roles/{id}")
    @ApiOperation(value = "删除角色信息")
    public Result del (@PathVariable Integer id){
        Role r = roleService.getById(id);
        if(r == null) {
            Assert.fail("该角色不存在，删除失败");
        }
        return Result.success(roleService.removeById(id));
    }

    @GetMapping("/roles")
    @ApiOperation(value = "查询所有的角色")
    public Result<List<Role>> listResult(
            @ApiParam(value = "角色名称",name = "name") @RequestParam(value = "name" ,required = false) String name
    ){
        List<Role> list = roleService.list(name);
        return Result.success(list);
    }



}

